//package com.seadragon.app.ksr.loader;
//
//import static com.seadragon.app.ksr.loader.StateLoader.appCtx;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.util.List;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipInputStream;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.seadragon.app.ksr.dao.EdgeDao;
//import com.seadragon.app.ksr.model.Edge;
//import com.seadragon.app.ksr.model.SHPFileRecord;
//import com.seadragon.app.ksr.tiger.DBFFileReader;
//import com.seadragon.app.ksr.tiger.SHPFileReader;
//
//@Component("edgeLoader")
//public class EdgeLoader {
//
//	private static Logger logger = LoggerFactory.getLogger(EdgeLoader.class);
//	private static final String SHP_FILE_EXTENSION = ".shp";
//	private static final String DBF_FILE_EXTENSION = ".dbf";
//	@Autowired
//	private EdgeDao edgeDao;
//	private static final String WORK_FOLDER = "work";
//	private DBFFileReader dbfFileReader;
//	private SHPFileReader shpFileReader;
//
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		if (args == null || args.length != 1) {
//			logger.error("Usage: EdgeLoader edge_file_directory");
//			return;
//		}
//		EdgeLoader loader = (EdgeLoader) appCtx.getBean("edgeLoader");
//		logger.debug("Start to load " + args[0]);
//		byte[] buffer = new byte[1024];
//		File file = new File(args[0]);
//		if (!file.exists()) {
//			logger.error("File doesn't exist: " + args[0]);
//			return;
//		}
//		if (!file.isDirectory()) {
//			logger.error("This is not a directory: " + args[0]);
//			return;
//		}
//		File[] files = file.listFiles();
//		File workFolder = new File(WORK_FOLDER);
//		if (workFolder.exists()) {
//			for (File f : workFolder.listFiles()) {
//				f.delete();
//			}
//		} else {
//			workFolder.mkdir();
//		}
//
//		for (int i = 0; i < files.length; i++) {
//			try {
//				ZipInputStream zis = new ZipInputStream(new FileInputStream(files[i]));
//				// get the zipped file list entry
//				ZipEntry ze = zis.getNextEntry();
//				// All the files have the same base name
//				String fileName = null;
//				while (ze != null) {
//					fileName = ze.getName();
//					File newFile = new File(workFolder + File.separator + fileName);
////
////					System.out.println("file unzip : " + newFile.getAbsoluteFile());
//
//					// create all non exists folders
//					// else you will hit FileNotFoundException for compressed
//					// folder
//					new File(newFile.getParent()).mkdirs();
//
//					FileOutputStream fos = new FileOutputStream(newFile);
//
//					int len;
//					while ((len = zis.read(buffer)) > 0) {
//						fos.write(buffer, 0, len);
//					}
//					fos.close();
//					ze = zis.getNextEntry();
//				}
//				zis.closeEntry();
//				zis.close();
//				loader.load(workFolder, getBaseName(fileName));
//			} catch (FileNotFoundException ex) {
//				logger.error(ex.getMessage(), ex);
//			} catch (IOException ex) {
//				logger.error(ex.getMessage(), ex);
//			}
//		}
//	}
//
////	public void load(String directory) {
////		logger.debug("Start to load " + directory);
////		byte[] buffer = new byte[1024];
////		File file = new File(directory);
////		if (!file.exists()) {
////			logger.error("File doesn't exist: " + directory);
////			return;
////		}
////		if (!file.isDirectory()) {
////			logger.error("This is not a directory: " + directory);
////			return;
////		}
////		File[] files = file.listFiles();
////		File workFolder = new File(WORK_FOLDER);
////		if (workFolder.exists()) {
////			for (File f : workFolder.listFiles()) {
////				f.delete();
////			}
////		} else {
////			workFolder.mkdir();
////		}
////
////		for (int i = 0; i < files.length; i++) {
////			try {
////				ZipInputStream zis = new ZipInputStream(new FileInputStream(files[i]));
////				// get the zipped file list entry
////				ZipEntry ze = zis.getNextEntry();
////				// All the files have the same base name
////				String fileName = null;
////				while (ze != null) {
////					fileName = ze.getName();
////					File newFile = new File(workFolder + File.separator + fileName);
//////
//////					System.out.println("file unzip : " + newFile.getAbsoluteFile());
////
////					// create all non exists folders
////					// else you will hit FileNotFoundException for compressed
////					// folder
////					new File(newFile.getParent()).mkdirs();
////
////					FileOutputStream fos = new FileOutputStream(newFile);
////
////					int len;
////					while ((len = zis.read(buffer)) > 0) {
////						fos.write(buffer, 0, len);
////					}
////					fos.close();
////					ze = zis.getNextEntry();
////				}
////				zis.closeEntry();
////				zis.close();
////				load(workFolder, getBaseName(fileName));
////			} catch (FileNotFoundException ex) {
////				logger.error(ex.getMessage(), ex);
////			} catch (IOException ex) {
////				logger.error(ex.getMessage(), ex);
////			}
////		}
////	}
//
//	 @Transactional
//	 public void load(File directory, String baseName) throws FileNotFoundException, IOException {
//		dbfFileReader = new DBFFileReader(new File(directory + File.separator + baseName + DBF_FILE_EXTENSION));
//		shpFileReader = new SHPFileReader(new File(directory + File.separator + baseName + SHP_FILE_EXTENSION));
//		logger.info("Processing " + directory + File.separator + baseName + DBF_FILE_EXTENSION);
//		while (dbfFileReader.hasNext() && shpFileReader.hasNext()) {
//			List<Object> dbfRecord = dbfFileReader.next();
//			SHPFileRecord shpRecord = shpFileReader.next();
//			int index = 0;
//			Edge edge = new Edge();
//			edge.setStateFips((String) dbfRecord.get(index++));
//			edge.setCountyFips((String) dbfRecord.get(index++));
//			edge.setTlId((Long) dbfRecord.get(index++));
//			edge.setTfIdLeft((Long) dbfRecord.get(index++));
//			edge.setTfIdRight((Long) dbfRecord.get(index++));
//			edge.setMtfClassCode((String) dbfRecord.get(index++));
//			edge.setFullName((String) dbfRecord.get(index++));
//			edge.setSmId((String) dbfRecord.get(index++));
//			edge.setLfromAdd((String) dbfRecord.get(index++));
//			edge.setLtoAdd((String) dbfRecord.get(index++));
//			edge.setRfromAdd((String) dbfRecord.get(index++));
//			edge.setRtoAdd((String) dbfRecord.get(index++));
//			edge.setZipLeft((String) dbfRecord.get(index++));
//			edge.setZipRight((String) dbfRecord.get(index++));
//			edge.setFeatCat((String) dbfRecord.get(index++));
//			String flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setHydroFlag(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setHydroFlag(true);
//			} else {
//				edge.setHydroFlag(null);
//			}
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setRailFlag(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setRailFlag(true);
//			} else {
//				edge.setRailFlag(null);
//			}
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setRoadFlag(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setRoadFlag(true);
//			} else {
//				edge.setRoadFlag(null);
//			}
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setOlfFlag(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setOlfFlag(true);
//			} else {
//				edge.setOlfFlag(null);
//			}
//			edge.setPassFlag((String) dbfRecord.get(index++));
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setDivRoadFlag(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setDivRoadFlag(true);
//			} else {
//				edge.setDivRoadFlag(null);
//			}
//			edge.setExtType((String) dbfRecord.get(index++));
//			edge.setTrackType((String) dbfRecord.get(index++));
//			edge.setDeckedRoad((String) dbfRecord.get(index++));
//			edge.setArtPath((String) dbfRecord.get(index++));
//			edge.setPersist((String) dbfRecord.get(index++));
//			edge.setGcseFlag((String) dbfRecord.get(index++));
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setOffsetLeft(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setOffsetLeft(true);
//			} else {
//				edge.setOffsetLeft(null);
//			}
//			flag = (String) dbfRecord.get(index++);
//			if ("Y".equalsIgnoreCase(flag)) {
//				edge.setOffsetRight(true);
//			} else if ("N".equalsIgnoreCase(flag)) {
//				edge.setOffsetRight(true);
//			} else {
//				edge.setOffsetRight(null);
//			}
//			edge.setTnIdFrom((Long) dbfRecord.get(index++));
//			edge.setTnIdTo((Long) dbfRecord.get(index++));
//			edge.setPoints(shpRecord.getRecordContent().getBlob());
//			edgeDao.save(edge);
//		}
//		dbfFileReader.close();
//		shpFileReader.close();
//	}
//
//	public static String getBaseName(String filename) {
//		return filename.substring(0, filename.indexOf('.'));
//	}
//}
